package com.howie.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.howie.gulimall.product.entity.AttrEntity;
import com.howie.gulimall.product.service.AttrAttrgroupRelationService;
import com.howie.gulimall.product.service.AttrService;
import com.howie.gulimall.product.service.CategoryService;
import com.howie.gulimall.product.vo.AttrGroupRelationVo;
import com.howie.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.howie.gulimall.product.entity.AttrGroupEntity;
import com.howie.gulimall.product.service.AttrGroupService;
import com.howie.common.utils.PageUtils;
import com.howie.common.utils.R;



/**
 * 属性分组
 * @author laizhenghua
 * @email 3299447929@qq.com
 * @date 2020-12-06 19:27:40
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 获取某分类下所有分组&关联属性
     * 请求路径：/product/attrgroup/{catelogId}/withattr
     * 请求方式：GET
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        // 1、查询当前分类下所有属性分组
        // 2、查出每个属性分组的所有属性
        List<AttrGroupWithAttrsVo> vos = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data",vos);
    }
    /**
     * 添加属性与分组的关联关系
     * 请求路径：/product/attrgroup/attr/relation
     * 前端传值(请求体)：[{"attrId":"xx","attrGroupId":"xx"}...]
     * 注解@RequestBody：读取http请求协议的请求体部分，并把相应的数据到绑定到controller中方法的参数上
     */
    @PostMapping(path = "/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> relationVoList){
        attrAttrgroupRelationService.saveBatch(relationVoList); // saveBatch() 批量保存
        return R.ok();
    }

    /**
     * 获取属性分组没有关联的其他属性
     * 请求路径：/product/attrgroup/{attrgroupId}/noattr/relation
     * 注解@RequestParam：将请求参数绑定到你控制器的方法参数上（是springMVC中接收普通参数的注解）(Request.getParameter("key"))
     */
    @GetMapping(path = "/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String,Object> params){
        PageUtils page = attrService.getNoRelationAttr(attrgroupId,params);
        return R.ok().put("page",page);
    }

    /**
     * 删除属性与分组的关联关系
     * 请求路径：/product/attrgroup/attr/relation/delete
     */
    @PostMapping(path = "/attr/relation/delete")
    public R deleteRelation(@RequestBody List<AttrGroupRelationVo> relationVos){
        // System.out.println("debug ==> " + relationVos);
        attrGroupService.deleteRelation(relationVos);
        return R.ok();
    }

    /**
     * 获取属性分组的关联的所有属性
     * 请求路径：/product/attrgroup/{attrgroupId}/attr/relation
     */
    @RequestMapping(path = "/{attrgroupId}/attr/relation",method = RequestMethod.GET)
    public R getAttrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> entities = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data",entities);
    }

    /**
     * 通过catelogId获取分类属性分组(catelogId就是category表里的id)
     * 请求路径：/product/attrgroup/list/{catelogId}
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable Long catelogId){
        // PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catelogId);
        // PageUtils分装了对分页数据的处理，默认分页数据都经过PageUtils处理后，在返回给前端。
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();

        Long[] path = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(path);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
